package com.leetcode;

/**
 * 377. 组合总和 Ⅳ
 *
 * @author fy
 * @date 2022/3/29 22:15
 */
public class Solution377 {

    private int[] combine;

    public int combinationSum41(int[] nums, int target) {
        combine = new int[target + 1];
        for (int i = 0; i <= target; i++) {
            combine[i] = -1;
        }
        findCombine(nums, target);
        return combine[target] != -1 ? combine[target] : 0;
    }

    private int findCombine(int[] nums, int target) {
        if (target == 0) {
            return 1;
        }
        if (combine[target] != -1) {
            return combine[target];
        }
        int res = 0;
        for (int i = 0; i < nums.length; i++) {
            if (target >= nums[i]) {
                res += findCombine(nums, target - nums[i]);
            }
        }
        combine[target] = res;
        return res;
    }


    public int combinationSum4(int[] nums, int target) {
        int[] count = new int[target + 1];
        count[0] = 1;
        for (int i = 0; i <= target; i++) {
            for (int j = 0; j < nums.length; j++) {
                if (i >= nums[j]) {
                    count[i] += count[i - nums[j]];
                }
            }
        }
        return count[target];
    }

    public static void main(String[] args) {

    }

}
